/* === LICENSE INFO ===
 * This source file is subject to the GPLv3 license that is bundled with this
 * package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl.txt
 * === end of LICENSE INFO === */

#include "MainWindow.h"
#include "ui_MainWindow.h"
#include "TestCaseDialog.h"
#include "RunTestPlanDialog.h"

#include <QTableWidgetItem>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ResetDisplay();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::ResetDisplay()
{
    this->setWindowTitle(tr("NetCom Simulator [New Test Plan]"));

    ui->txtlineTestPlanName->setText(tr("New Test Plan"));
    ui->txtlineTesPlanVersion->setText(tr("(Non-versioned)"));
    ui->txtplainTestPlanRemarks->setPlainText(tr(""));

    ui->btnlinkTestPlanVersion->setEnabled(false);
    ui->btnDeleteTestCase->setEnabled(false);
    ui->btnOpenTestCase->setEnabled(false);

    // Reset the table of Test Case List.
    // 4 colums: Test Case, Protocol, Role, Version.
    ui->tableTestCaseList->setRowCount(0);
    ui->tableTestCaseList->setColumnCount(4);

    ui->tableTestCaseList->setColumnWidth(0, 160);
    ui->tableTestCaseList->setColumnWidth(1, 80);
    ui->tableTestCaseList->setColumnWidth(2, 80);
    ui->tableTestCaseList->setColumnWidth(3, 80);
}

// Slots
void MainWindow::on_actionNew_triggered()
{
    ResetDisplay();
    ui->statusbar->showMessage(tr("New Test Plan..."), 5000);
}

void MainWindow::on_actionOpen_triggered()
{
    QString fileName
        = QFileDialog::getOpenFileName(this, tr("Open Test Plan"), tr(""),
                                       tr("NetCom Test Plan (*.tpl);; All files (*.*)"));

    if (fileName=="")
        ui->statusbar->showMessage(tr("No Test Plan file is selected."), 5000);
    else
        ui->statusbar->showMessage(tr("Test Plan opened: ") + fileName, 5000);
}

void MainWindow::on_actionSave_triggered()
{
    QString fileName
        = QFileDialog::getSaveFileName(this, tr("Save Test Plan"), tr(""),
                                       tr("NetCom Test Plan (*.tpl);; All files (*.*)"));

    if (fileName=="")
        ui->statusbar->showMessage(tr("No Test Plan file is selected."), 5000);
    else
        ui->statusbar->showMessage(tr("Test Plan saved: ") + fileName, 5000);
}

void MainWindow::on_actionExit_triggered()
{
    close();
}

void MainWindow::on_actionHelp_triggered()
{
    ui->statusbar->showMessage(tr("Help content is not available yet!"), 5000);
    QMessageBox::information(this, tr("Help Topics"), tr("Help content is not available yet!"));
}

void MainWindow::on_actionAbout_triggered()
{
    QMessageBox::about(this, tr("About NetCom Simulator"),
                       tr("<b>NetCom Simulator</b><br>A network messaging testing framework."));
}

void MainWindow::on_btnAddTestCase_clicked()
{
    TestCaseDialog dialog;
    dialog.exec();
}

void MainWindow::on_btnRunTest_clicked()
{
    RunTestPlanDialog dialog;
    dialog.exec();
}

void MainWindow::on_btnSaveTestPlan_clicked()
{
    on_actionSave_triggered();
}
